<?php
session_start();
include_once '../pushing.php';

if ($pushTime == true) {
	if ($_SESSION['UserId'] != 3)
		header('Location: push.php');
}

if(!isset($_SESSION['UserId']))
{
	header('Location: login.php');
	die();
}

# ini_set('display_errors', '1');

require_once '../mysqlConnect.php'; //mysqli database connect
require_once '../Functions/ViewPatientController.php';

function getPatientInfobyID($id) {
	//returns all fields from patient table

	$conn = GetConnection();
	$sql = "SELECT * FROM Patient WHERE ID ='$id'";
	$value = $conn->query($sql);
	$rs = $value->fetch_assoc();
	$conn->close();
	return $rs;
}

function getTestName($testId) {
	$conn = GetConnection();
	$sql = "SELECT Name FROM Test WHERE ID = '$testId'";
	$value = $conn->query($sql);
	$rs = $value->fetch_assoc();
	$real = $rs['Name'];
	$conn->close();
	return $real;
}


function getMedicationNames($id) {
	$conn = GetConnection();
	$results = $conn->query("SELECT Name FROM Medication where ID = " . $id);
	$rs = $results->fetch_assoc();
	$return = $rs['Name'];
	$conn->close();
	return $return;
}

$patientInfo = getPatientInfobyID($_REQUEST['PatId']);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View Patient</title>
<script
	src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"
	type="text/javascript"></script>
<script
	src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="../Styler.css" />
<script src="../JavaScript/js_basic_functions.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function(){

	setCollapseBind("basicInfoClick", "BasicInfocollapse");
	setCollapseBind("icdClick", "icdCollapse");
	setCollapseBind("diagnosticInfoClick", "diagnosticInfoCollapse");
	setCollapseBind("prescriptionInfoClick", "prescriptionInfoCollapse");
	setCollapseBind("psychologicalTestClick", "psychologicalTestCollapse");
	setCollapseBind("physicalSymptomClick", "physicalSymptomCollapse");
	setCollapseBind("neuroPsychologicalClick", "neuroPsychologicalCollapse");

	//$("#BasicInfocollapse").slideToggle('slow');
});
</script>

</head>
<body>

	<div id="header">

		<div class="pageTitleText">View Patient</div>
		<div class="pageTitleText" style="font-size: 1em;">
			<span class="pageTitleSub">Patient: <?php echo $patientInfo['PatID'];?>
			</span> <span class="pageTitleSub">Date Diagnosed: <?php echo $patientInfo['DateDiagnosed'];?>
			</span>
		</div>
	</div>

	<div class="viewOptions">
		<span class="loginButton"> <a
			href="Edit Patient.php?PatId=<?php echo $_GET['PatId'];?>">Edit
				Information</a>
		</span> 
		
		<span class="loginButton"> <a
			href="Save Information.php?PatId=<?php echo $_GET['PatId']?>">Save</a>
		</span>
	</div>


	<div id="mainMenu" style="height: 466px;">
		<ul style="list-style: none outside none;">
			<li>
				<a href="choosePatient.php?SID=8" style="font-weight: bold; color: #7C3709;">Choose New Patient</a>
			</li>

			<li>
	         	<a href="Add_ICD9.php?PatId=<?php echo $_GET['PatId'];?>">Patient ICD9</a>
	        </li>

			<li>
				<a href="Add Prescription.php?PatId=<?php echo $_GET['PatId'];?>">Patient Prescription</a>
			</li>

			<li>
				<a href="Physical Symptoms.php?PatId=<?php echo $_GET['PatId'];?>">Physical Symptoms</a>
			</li>

			<li>
				<a href="Neuropsychological Symptoms.php?PatId=<?php echo $_GET['PatId'];?>">Neuropsychological Symptoms</a>
			</li>

			<li>
				<a href="Psychological Test.php?PatId=<?php echo $_GET['PatId'];?>">Psychological Test</a>
			</li>

			<li>
				<a href="Diagnostic Test.php?PatId=<?php echo $_GET['PatId'];?>">Diagnostic Test</a>
			</li>
			
			<li>
	        	<a href="viewPatient.php?PatId=<?php echo $_GET['PatId'];?>" style="font-weight: bold; color: #FB7D08;">View Patient Information</a>
	        </li>

			<li>
				<a href="Edit Patient.php?PatId=<?php echo $_GET['PatId'];?>">Edit Patient Information</a>
			</li>
			
			<li>
	        	<a href="Save Information.php?PatId=<?php echo $_GET['PatId'];?>">Save Patient Information</a>
	        </li>

			<li style="border-bottom: 1px solid #FD9D45;">
				<del><!--<a href="query.php">-->Query<!--</a>--></del>
			</li>

			<li><a href="home.php">Home</a>
			</li>

			<li><a href="logout.php">Logout</a>
			</li>
		</ul>
	</div>


	<div class="formContainer">




		<div class="boxStyle">
			<div class="boxStyleTitle" id="basicInfoClick">Basic Information</div>
			<div class="inboxStyle" id="BasicInfocollapse">
				<div id="basicInfoTable">
					<table id="table1" border="1">


						<tr>
							<th>Date of Birth</th>
							<td><?php echo $patientInfo['DOB'];?></td>
						</tr>
						<tr>
							<th>Gender</th>
							<td><?php echo $patientInfo['Gender'];?></td>
						</tr>
						<tr>
							<th>Relationship Status</th>
							<td><?php if($patientInfo['RelationshipStatus'] == 0) echo 'Single'; else echo 'In a Relationship';?>
							</td>
						</tr>
						<tr>
							<th>Occupation Status</th>
							<td><?php if($patientInfo['OccupationStatus'] == 0) echo 'Unemployed'; else echo 'Employed';?>
							</td>
						</tr>
		                <tr>
		                	<th>Ticks in Work Environment</th>
		                	<td><?php if($patientInfo['WorkHabitat'] == 1) echo 'Yes'; else echo 'No';?></td>
		                </tr>
		                <tr>
		                	<th>Ticks in Hunting Environment</th>
		                	<td><?php if($patientInfo['HuntHabitat'] == 1) echo 'Yes'; else echo 'No';?></td>
		                </tr>
		                 <tr>
		                	<th>Ticks in Play Environment</th>
		                	<td><?php if($patientInfo['PlayHabitat'] == 1) echo 'Yes'; else echo 'No';?></td>
		                </tr>
		                <tr>
		                	<th>Ticks in Garden Environment</th>
		                	<td><?php if($patientInfo['GardenHabitat'] == 1) echo 'Yes'; else echo 'No';?></td>
		                </tr>
		                <tr>
		                	<th>Tickbite Caused Original Disease</th>
		                	<td><?php if($patientInfo['TickBite'] == 1) echo 'Yes'; else echo 'No';?></td>
		                </tr>
		                <tr>
		                	<th>Currently Has a Rash</th>
		                	<td><?php if($patientInfo['Rash'] == 1) echo 'Yes'; else echo 'No';?></td>
		                </tr>
						</table>
				</div>
			</div>
		</div>



	<div class="boxStyle">
			<div class="boxStyleTitle" id="icdClick">ICD9 Information</div>		
	        	<?php
	        	$icd_objects = fetchICDObjects($_GET['PatId']);
	        	?>
		            <div class="inboxStyle" id="icdCollapse">
			            <div class="tableDiv">
			            <?php if(count($icd_objects) > 0) { //if the results pulled from the database are not null?>
			           		<table class="tableClass">
			                     
			                    <tr style="background-color: #FFB93E;">
			                    	<th>Code</th>
			                    	<th>Name</th>
			                    	<th>Date Assigned</th>
			                    </tr>
			                    
			                    <?php $DiagnosticTableColor = 1; // counter for background color change?>
			                    <?php foreach ($icd_objects as $objectArray){ ?>
			                    	<tr>
				                    	<td style="<?php if ($DiagnosticTableColor % 2 == 0)	echo "background-color:#A1DFE8;";?>"><?php echo $objectArray['code'];?></td>
	  							    	<td style="<?php if ($DiagnosticTableColor % 2 == 0)	echo "background-color:#A1DFE8;";?>width:40%;"><?php echo $objectArray['name'];?></td>
                    			       	<td style="<?php if ($DiagnosticTableColor % 2 == 0)	echo "background-color:#A1DFE8;";?>"><?php echo $objectArray['date_given'];?></td>
                    			    </tr>
									<?php $DiagnosticTableColor++; // increases background color counter
			                    } ?>
			                    
			                </table>
			                <?php } else {
			                	echo "No Results";
			                }?>
			            </div>
		            </div>
	        </div>




		<div class="boxStyle">
			<div class="boxStyleTitle" id="diagnosticInfoClick">Diagnostic Test
				Information</div>		
			
	        	<?php 
	        	
	        	$conn = GetConnection();
	        	$sqlColumnNames = "SELECT * FROM medical_test_questions ORDER BY qnumber";
	        	$columnNames = $conn->query($sqlColumnNames);
	        	
	        	$sqlAnswers = "SELECT * FROM medical_test_answers WHERE patient_id = " . $_GET['PatId'] . " ORDER BY record_date desc";
	        	$answers = $conn->query($sqlAnswers);
	        	?>
	        	
	        	
		            <div class="inboxStyle" id="diagnosticInfoCollapse">
			            <div class="tableDiv">
			            <?php if($answers->num_rows >= 1) { //if the results pulled from the database are not null?>
			           		<table class="tableClass">
			                     
			                    <tr style="background-color: #FFB93E;">
			                    <?php while ($columnName = $columnNames->fetch_assoc()) { ?>
			                    	<th><?php echo $columnName['text'];?></th>
			            		<?php }?>
			                    </tr>
			                    
			                    <?php $DiagnosticTableColor = 1; // counter for background color change?>
			                    <?php while($answer = $answers->fetch_assoc()){
									if ($DiagnosticTableColor % 2 == 0)	{ ?>
					            		<tr>
					                    	<td style="background-color:#A1DFE8; width:40%;"><?php echo getTestName($answer['testname'])?></td>
					                    	<td style="background-color:#A1DFE8;"><?php echo $answer['result1']?></td>
					                    	<td style="background-color:#A1DFE8;"><?php echo $answer['result2']?></td>
					                    	<td style="background-color:#A1DFE8;"><?php echo $answer['record_date']?></td>
					                    </tr>
				                    <?php } 
				                    else { ?>
										<tr>
					                    	<td style="width: 40%;"><?php echo getTestName($answer['testname'])?></td>
					                    	<td><?php echo $answer['result1']?></td>
					                    	<td><?php echo $answer['result2']?></td>
					                    	<td><?php echo $answer['record_date']?></td>
					                    </tr>
									<?php }
									 
									$DiagnosticTableColor++; // increases background color counter
			                    } ?>
			                    
			                </table>
			                <?php } else {
			                	echo "No Results";
			                }?>
			            </div>
		            </div>
	        </div>






		<div class="boxStyle">
			<div class="boxStyleTitle" id="prescriptionInfoClick">Prescription
				Information</div>
			
			
			
			
			
	        	<?php 
	        	$sqlColumnNames = "SELECT * FROM prescription_questions ORDER BY qnumber";
	        	$columnNames = $conn->query($sqlColumnNames);
	        	
	        	$sqlAnswers = "SELECT * FROM prescription_records WHERE patient_id = " . $_GET['PatId'] . " ORDER BY end_date desc";
	        	$answers = $conn->query($sqlAnswers);
	        	
	        	?>
		            <div class="inboxStyle" id="prescriptionInfoCollapse">
			            <div class="tableDiv">
				            <?php if($answers->num_rows >= 1) {?>
				            	<table class="tableClass">
				                    
				                    <tr style="background-color: #FFB93E">
				                    <?php while ($columnName = $columnNames->fetch_assoc()) { ?>
				                    	<th><?php echo $columnName['text'];?></th>
				            		<?php }?>
				                    </tr>
				                    
				                    <?php $prescriptionTableColor = 1; ?>
				                    <?php while($presInfo = $answers->fetch_assoc()) { 
									if ($prescriptionTableColor % 2 == 0) {?>
					                    <tr>
					                    	<td style="background-color:#A1DFE8;"><?php echo getMedicationNames($presInfo['medication_id']);?></td>
					                    	<td style="background-color:#A1DFE8;"><?php echo $presInfo['dosage'];?> mg</td>
					                    	<td style="background-color:#A1DFE8;"><?php echo $presInfo['start_date'];?></td>
					                    	<td style="background-color:#A1DFE8;"><?php echo $presInfo['end_date'];?></td>
					                    </tr>
					                    <?php } 
					                    else { ?>
					                     <tr>
					                    	<td><?php echo getMedicationNames($presInfo['medication_id']);?></td>
					                    	<td><?php echo $presInfo['dosage'];?> mg</td>
					                    	<td><?php echo $presInfo['start_date'];?></td>
					                    	<td><?php echo $presInfo['end_date'];?></td>
					                     </tr>
					                    <?php }
				                                        
				                    	$prescriptionTableColor++; 
				                    } ?>
				                     
				                </table>
				           <?php } else {
				           	echo "No Results";
				           } ?>
			           </div>
           		</div>
           </div>






		<div class="boxStyle" id="psychologicalTestBox">
			<div class="boxStyleTitle" id="psychologicalTestClick">Psychological
				Test Information</div>
			
	        	<?php
	        	$sqlColumnNames = "SELECT * FROM psychological_test_questions ORDER BY qnumber";
	        	$columnNames = $conn->query($sqlColumnNames);
	        	
	        	$sqlAnswers = "SELECT * FROM psychological_test_answers WHERE patient_id = " . $_GET['PatId'] . " ORDER BY record_date desc";
	        	$answers = $conn->query($sqlAnswers);
	        	
	        	$sqlAnswerDesc = "SELECT * FROM answer_options";
	        	$descriptions = $conn->query($sqlAnswerDesc);
	        	
	        	while ($desc = $descriptions->fetch_assoc())
	        		$answerDescriptions[$desc['type']] = $desc['description'];
	        	?>
	            <div class="inboxStyle" id="psychologicalTestCollapse">
		             <div class="tableDiv">
		            <?php if($answers->num_rows >= 1) {?>
		            	<table class="tableClass">
		
		
		                <tr style="background-color: #FFB93E">
		                <?php while ($columnName = $columnNames->fetch_assoc()) { ?>
		                	<th><?php echo $columnName['text']?></th>
		            	<?php }?>
		                </tr>
		                
		                 <?php $countPsychInfo = 1; ?>
		                 <?php while ($Info = $answers->fetch_assoc()) {?>
		        		<?php if ($countPsychInfo % 2 == 0)
									$td = '<td style="background-color:#A1DFE8;">';
		        				else
		        					$td = '<td>'; 
		        			?>
		                	<tr>
		                		<?php
									echo $td . $Info['qnumber1'] . ' : ' . $answerDescriptions[$Info['qnumber1']] . '</td>';
									echo $td . $Info['qnumber2'] . ' : ' . $answerDescriptions[$Info['qnumber2']] . '</td>';
									echo $td . $Info['qnumber3'] . '</td>';
									echo $td . $Info['global'] . '</td>';
									echo $td . $Info['record_date'] . '</td>';
								?>
							</tr>
		                <?php 
							$countPsychInfo++;
						
							
		                } ?>
		                
		                 
		                </table>
		                 <?php }  else {
		                	echo "No Results";
		                }?>
		            </div>
				</div>
            </div>





		<!-- table containing physical symptom information: arthritis/joint pain, fatigue, dizziness, etc -->
		<!--  finished editing -->
		<div class="boxStyle"
			style="position: relative; left: -250px; min-width: 140%;"
			id="physicalSymtomBox">
			<div class="boxStyleTitle" id="physicalSymptomClick">Physical Symptom
				Information</div>
			
			
			
			

		        <?php 
	        	$sqlColumnNames = "SELECT * FROM physical_symptom_questions ORDER BY qnumber";
	        	$columnNames = $conn->query($sqlColumnNames);
	        	
	        	$sqlAnswers = "SELECT * FROM physical_symptom_answers WHERE patient_id = " . $_GET['PatId'] . " ORDER BY record_date desc";
	        	$answers = $conn->query($sqlAnswers);
	        	?>
	            <div class="inboxStyle" id="physicalSymptomCollapse">
		             <div class="tableDiv">
		            <?php if ($answers->num_rows >= 1) {?>
		            	<table class="tableClass">
		
		
		                <tr style="background-color: #FFB93E">
		                <?php while ($columnName = $columnNames->fetch_assoc()) { ?>
		                	<th><?php echo $columnName['text'];?></th>
		            	<?php }?>
		                </tr>
		                
		                 <?php $countPsychInfo = 1; ?>
		                 <?php while ($Info = $answers->fetch_assoc()) {?>
		                 <?php 
		                 	next($Info); //sets pointer ahead of id
		                 	next($Info); //sets pointer ahead of patient_id
		                 ?>
		                 
		        		<?php if ($countPsychInfo % 2 == 0) {?>
		                	<tr>
								<?php
		        					for ($i = 0; $i < count($Info) - 2; $i++) { ?>
			                			<td style="background-color:#A1DFE8;"><?php echo $Info[key($Info)];next($Info);?></td>
		        					<?php } ?>
		                	</tr>
		                <?php } 
		                else { ?>
							<tr>
								<?php
		        					for ($i = 0; $i < count($Info) - 2; $i++) { ?>
			                			<td><?php echo $Info[key($Info)];next($Info);?></td>
		        					<?php } ?>
							</tr>	
						<?php }
						
							$countPsychInfo++;?>
							
		                <?php } ?>
		                
		                </table>
		                 <?php }  else {
		                 echo "No Results";
		                 }
		              ?>
		            </div>
				</div>
            </div>





		<!-- neuropsychological symptoms table: attention impairment, anxiety/panic, etc -->
		<div class="boxStyle"
			style="position: relative; left: -250px; min-width: 140%;"
			id="neuroPsychologicalBox">
			<div class="boxStyleTitle" id="neuroPsychologicalClick">
				Neuro-Psychological Symptom Information</div>
			
			
			
			

		        <?php 
	        	$sqlColumnNames = "SELECT * FROM neuropsychological_symptom_questions ORDER BY qnumber";
	        	$columnNames = $conn->query($sqlColumnNames);
	        	
	        	$sqlAnswers = "SELECT * FROM neuropsychological_symptom_answers WHERE patient_id = " . $_GET['PatId'] . " ORDER BY record_date desc";
	        	$answers = $conn->query($sqlAnswers);
	        	?>
	            <div class="inboxStyle" id="neuroPsychologicalCollapse">
		             <div class="tableDiv">
		            <?php if($answers->num_rows >= 1) {?>
		            	<table class="tableClass">
		
		
		                <tr style="background-color: #FFB93E">
		                <?php while ($columnName = $columnNames->fetch_assoc()) { ?>
		                	<th><?php echo $columnName['text'];?></th>
		            	<?php }?>
		                </tr>
		                
		                 <?php $countPsychInfo = 1; ?>
		                 <?php while ($Info = $answers->fetch_assoc()) {?>
		        		
		                 
		                 <?php if ($countPsychInfo % 2 == 0) {?>
		                	<tr>
								<?php
									next($Info); //sets pointer ahead of id
									next($Info); //sets pointer ahead of patient_id
									
		        					for ($i = 0; $i < count($Info) - 2; $i++) { ?>
			                			<td style="background-color:#A1DFE8;"><?php echo $Info[key($Info)];next($Info);?></td>
		        					<?php } ?>
		                	</tr>
		                <?php } 
		                else { ?>
							<tr>
								<?php
									next($Info); //sets pointer ahead of id
									next($Info); //sets pointer ahead of patient_id
									
		        					for ($i = 0; $i < count($Info) - 2; $i++) { ?>
			                			<td><?php echo $Info[key($Info)];next($Info);?></td>
		        					<?php } ?>
							</tr>	
						<?php }
						
							$countPsychInfo++;?>
							
		                <?php } ?>
		                
		                </table>
		                 <?php }  else {
		                	echo "No Results";
		                }?>
		            </div>
				</div>
            </div>










	</div>
</body>
</html>
